﻿#include "ssMesh.h"
#include "file/ssReadFile.h"
#include "console/ssConsole.h"
#include "string/ssStrfunc.h"

namespace StarSeeker
{

ssMesh::ssMesh(const char* name)
:ssIRes(name)
{
}

ssMesh::~ssMesh()
{
}

bool ssMesh::LoadFromFile(const char* uri)
{
	if (m_data.LoadFromFile(uri))
	{
		if (m_data.m_vertexArr.Count() > 0)
			CreateVertex(m_data.m_vertexArr.Memory(), m_data.m_vertexArr.Count());
		if (m_data.m_indexArr.Count() > 0)
			CreateIndex(SS_INDEX_16, m_data.m_indexArr.Memory(), m_data.m_indexArr.Count());
		if (m_data.m_texCoordArr.Count() > 0)
			CreateTexture2d(m_data.m_texCoordArr.Memory(), m_data.m_texCoordArr.Count());
		if (m_data.m_normalArr.Count() > 0)
			CreateNormal(m_data.m_normalArr.Memory(), m_data.m_normalArr.Count());
		if (m_data.m_tangentArr.Count() > 0)
			CreateTangent(m_data.m_tangentArr.Memory(), m_data.m_tangentArr.Count());
		return true;
	}
	return false;
}

void ssMesh::CreateVertex(const ssfloat* pv, u32 count)
{
	if (pv && count)
	{
		m_vertex.CreateFloat(pv, count);
	}
}

void ssMesh::CreateIndex(u16 bit, const void* pi, u32 count)
{
	if (pi && count)
	{
		m_index.Create(bit, pi, count);
	}
}

void ssMesh::CreateNormal(const ssfloat* pn, u32 count)
{
	if (pn && count)
	{
		m_normal.CreateFloat(pn, count);
	}
}

void ssMesh::CreateTexture2d(const ssfloat* pt, u32 count)
{
	if (pt && count)
	{
		m_texture.CreateFloat(pt, count);
	}
}

void ssMesh::CreateColor(const ssfloat* pc, u32 count)
{
	if (pc && count)
	{
		m_color.CreateFloat(pc, count);
	}
}

void ssMesh::CreateTangent(const ssfloat * pt, u32 count)
{
	if (pt && count)
	{
		m_tangent.CreateFloat(pt, count);
	}
}

StarSeeker::ssVBOBind* ssMesh::GetVertexVBO()
{
	return m_vertex.m_vbo.IsValid() ? &m_vertex : 0;
}

StarSeeker::ssIBOBind* ssMesh::GetVertexIBO()
{
	return m_index.m_ibo.IsValid() ? &m_index : 0;
}

StarSeeker::ssVBOBind* ssMesh::GetNormalVBO()
{
	return m_normal.m_vbo.IsValid() ? &m_normal : 0;
}

StarSeeker::ssVBOBind* ssMesh::GetTexture2dVBO()
{
	return m_texture.m_vbo.IsValid() ? &m_texture : 0;
}

StarSeeker::ssVBOBind* ssMesh::GetColorVBO()
{
	return m_color.m_vbo.IsValid() ? &m_color : 0;
}

ssVBOBind * ssMesh::GetTangentVBO()
{
	return m_tangent.m_vbo.IsValid() ? &m_tangent : 0;
}

}
